package com.example.zlloggingservice.mq.consumer;

import com.example.zlloggingservice.entity.OperationLog;
import com.example.zlloggingservice.mapper.OperationLogMapper;
import com.example.zlloggingservice.mq.message.OperationLogMessage;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
 * @Author zhenliu（孙凌岳）
 * @Description 操作日志消费者
 * @Date 2025/6/19 15:13
 * @Version 1.0
 */
@Component
@RequiredArgsConstructor

@Slf4j
public class OperationLogConsumer {

    private final OperationLogMapper operationLogMapper;

    /**
     * 监听操作日志队列
     */
    @RabbitListener(queues = "log.queue")
    public void receive(OperationLogMessage message) {
        log.info("[日志服务] 接收到日志消息：{}", message);

        OperationLog operationLog = new OperationLog();
        operationLog.setOperId(message.getOperId());
        operationLog.setUserId(message.getUserId());
        operationLog.setUsername(message.getUsername());
        operationLog.setAction(message.getAction());
        operationLog.setUrl(message.getUrl());
        operationLog.setIp(message.getIp());
        operationLog.setDetail(message.getDetail());
        operationLog.setStatus(message.getStatus());

        log.info(">>> operId: " + operationLog.getOperId()); // 确认不为 null

        operationLogMapper.insert(operationLog);
        log.info("[日志服务] 操作日志保存成功");
    }
}